// var i;
// var AL_GAME_HUA_AddPrefab = require("./AL_GAME_HUA_AddPrefab");
// var AL_GAME_HUA_Instance = require("./AL_GAME_HUA_Instance");
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var AL_GAME_HUA_PopupSelf = require("./AL_GAME_HUA_PopupSelf");
// var AL_GAME_HUA_AwardConfig = require("./AL_GAME_HUA_AwardConfig");
// var AL_GAME_HUA_EventConfig = require("./AL_GAME_HUA_EventConfig");
// var AL_GAME_HUA_PropConfig = require("./AL_GAME_HUA_PropConfig");
// var AL_GAME_HUA_CurrencyData = require("./AL_GAME_HUA_CurrencyData");
// var AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
// var AL_GAME_HUA_GoodsConfig = require("./AL_GAME_HUA_GoodsConfig");
// var v = cc._decorator;
// var D = v.ccclass;
// var L = v.property;
// var b =
//     (v.menu,
//     (function (e) {
//         function t() {
//             var t = (null !== e && e.apply(this, arguments)) || this;
//             t.award = [];
//             t.sprite_award_icon = null;
//             t.award_node = [];
//             t.btn_node = null;
//             t.ske = null;
//             t.ske2 = null;
//             t.types = !0;
//             t.speed = 1500;
//             t.goldary = [];
//             t.posary = [cc.v3(-200, 95), cc.v3(0, 160), cc.v3(200, 95)];
//             t.clicktype = !1;
//             return t;
//         }
//         __extends(t, e);
//         t.prototype.xianshi_dongzuo = function () {
//             return Promise.resolve();
//         };
//         t.prototype.yincang_dongzuo = function () {
//             return Promise.resolve();
//         };
//         t.prototype.start = function () {
//             AL_GAME_HUA_Instance.Instance.xianshi_dingbu_tiao({
//                 target: this.node,
//                 showGold: !0
//             });
//         };
//         t.prototype.onEnable = function () {
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
//                 node: this.node,
//                 hide: !0
//             });
//         };
//         t.prototype.onDisable = function () {
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
//                 node: this.node,
//                 hide: !1
//             });
//         };
//         t.prototype.zhixing_chushihua_chuli = function (e) {
//             var t = this;
//             this.options = e;
//             var n;
//             var o = AL_GAME_HUA_GameData.GameData.getInstance();
//             var i = AL_GAME_HUA_AwardConfig.AwardConfig.getInstance();
//             this.ske.node.active = !1;
//             this.ske2.node.active = !1;
//             e.type == AL_GAME_HUA_Define.Define.AwardType.LEVEL
//                 ? ((n = i.genju_jishu_huoqu_jianli_xinxi(o.huoqu_jiangli_shuliang())), this.ske)
//                 : e.type == AL_GAME_HUA_Define.Define.AwardType.CHALLENGE
//                 ? ((n = i.huoqu_tiaozhan_jianli_xinxi()), this.ske2)
//                 : e.type == AL_GAME_HUA_Define.Define.AwardType.SPECIAL
//                 ? ((n = i.genju_jishu_huoqu_jianli_xinxi(o.huoqu_teshu_jiangli_shuliang())), this.ske)
//                 : e.type == AL_GAME_HUA_Define.Define.AwardType.STAR && (n = i.genju_jishu_huoqu_jianli_xinxi(o.huoqu_xingxing_hezi_shuliang()));
//             this.award[0].string = "x" + n.Award1.toString();
//             this.award[1].string = "x" + n.Number.toString();
//             var r = {
//                 0: 2,
//                 1: 3,
//                 2: 4,
//                 3: 5,
//                 4: 6,
//                 5: 7,
//                 6: 8
//             }[n.Award2];
//             AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance()
//                 .jiazai_tubiao_icon(r)
//                 .then(function (e) {
//                     if (cc.isValid(t)) {
//                         t.sprite_award_icon.spriteFrame = e;
//                         t.sprite_award_icon.addAutoReleaseAssets(e);
//                         AL_GAME_HUA_Common.Common.uniform_scale(t.sprite_award_icon, 80, 80);
//                     }
//                 });
//             AL_GAME_HUA_Common.Common.bofang_yinxiao("box_fall", this);
//             this.ske.node.active = !0;
//             this.ske.setAnimation(0, "show1", !1);
//             var a = this.ske.addAnimation(0, "show2", !1);
//             this.ske.addAnimation(0, "idle", !0);
//             this.scheduleOnce(function () {
//                 AL_GAME_HUA_Common.Common.bofang_yinxiao("game_openbox", t);
//                 for (var e = 0; e < t.award.length; e++) {
//                     t.chuli_jiedian_yidong_donghua(t.award[e].node.parent);
//                 }
//             }, 1);
//             this.ske.setTrackCompleteListener(a, function () {
//                 cc.tween(t.btn_node)
//                     .delay(0.5)
//                     .by(
//                         0.15,
//                         {
//                             y: 465
//                         },
//                         {
//                             easing: "backOut"
//                         }
//                     )
//                     .start();
//                 cc.tween(t.ske.node)
//                     .delay(0.5)
//                     .by(
//                         0.4,
//                         {
//                             y: -cc.winSize.height / 2 - 150
//                         },
//                         {
//                             easing: "backIn"
//                         }
//                     )
//                     .start();
//             });
//         };
//         t.prototype.recieve = function (e, t) {
//             var n;
//             var o = this;
//             var i = AL_GAME_HUA_CurrencyData.CurrencyData.getInstance();
//             var r = AL_GAME_HUA_GameData.GameData.getInstance();
//             var a = AL_GAME_HUA_AwardConfig.AwardConfig.getInstance();
//             var s = this.options.type == AL_GAME_HUA_Define.Define.AwardType.LEVEL ? r.huoqu_jiangli_shuliang() : r.huoqu_xingxing_hezi_shuliang();
//             var c = a.genju_jishu_huoqu_jianli_xinxi(s);
//             var u = c.Award1;
//             var p = c.Award2;
//             var d = c.Number;
//             var v =
//                 (((n = {})[AL_GAME_HUA_PropConfig.PropId.TIPS_ENTER] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_magicStick),
//                 (n[AL_GAME_HUA_PropConfig.PropId.TIME_ENTER] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_overtime),
//                 (n[AL_GAME_HUA_PropConfig.PropId.STAR_ENTER] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_doubleStar),
//                 (n[AL_GAME_HUA_PropConfig.PropId.TIPS_GAME] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt),
//                 (n[AL_GAME_HUA_PropConfig.PropId.REFRESH_GAME] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset),
//                 (n[AL_GAME_HUA_PropConfig.PropId.TIME_GAME] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze),
//                 (n[AL_GAME_HUA_PropConfig.PropId.CHANGE_GAME] = AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush),
//                 n);
//             var D = [
//                 {
//                     goods: AL_GAME_HUA_GoodsConfig.GoodsId.gold,
//                     num: u * e
//                 },
//                 {
//                     goods: v[p],
//                     num: d * e
//                 }
//             ];
//             var L = this.options.chuli_lingqu;
//             AL_GAME_HUA_AddPrefab.AddPrefab.zhixing_feixing_jinbi({
//                 fromNode: this.award_node[0],
//                 count: 5,
//                 onEnd: function () {
//                     i.tianjia_huobi({
//                         type: 0,
//                         num: u * e
//                     });
//                     AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
//                         source:
//                             o.options.type == AL_GAME_HUA_Define.Define.AwardType.STAR
//                                 ? AL_GAME_HUA_EventConfig.AssetsSource.starbox
//                                 : AL_GAME_HUA_EventConfig.AssetsSource.giftbox,
//                         action:
//                             e > 1
//                                 ? 2 == t
//                                     ? AL_GAME_HUA_EventConfig.AssetsAction.video_get
//                                     : AL_GAME_HUA_EventConfig.AssetsAction.share_get
//                                 : AL_GAME_HUA_EventConfig.AssetsAction.get,
//                         goodsList: D
//                     });
//                     if (L) {
//                         L();
//                     }
//                     if (cc.isValid(o)) {
//                         o.close();
//                     }
//                 }
//             });
//             r.genggai_prop_shuliang(p, d * e);
//             this.options.type == AL_GAME_HUA_Define.Define.AwardType.LEVEL
//                 ? r.shezhi_jiangli_shuliang()
//                 : this.options.type == AL_GAME_HUA_Define.Define.AwardType.SPECIAL
//                 ? r.shezhi_teshu_jiangli_shuliang()
//                 : this.options.type == AL_GAME_HUA_Define.Define.AwardType.STAR && r.tianjia_xingxing_hezi_shuliang();
//         };
//         t.prototype.handle_button_callback = function (e, t) {
//             var n = this;
//             switch (t) {
//                 case "recieve":
//                     this.recieve(1, 0);
//                     this.xianshi_block_top();
//                     break;
//                 case "video":
//                     this.xianshi_block_top();
//                     AL_GAME_HUA_Common.Common.showVideo({
//                         success: function (e) {
//                             n.recieve(2, e.type);
//                         },
//                         fail: function () {
//                             n.yincang_block_top();
//                             AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_tishi_jiedian_mianban({
//                                 str: "暂无视频"
//                             });
//                         }
//                     });
//             }
//         };
//         t.prototype.chuli_jiedian_yidong_donghua = function (e) {
//             var t = this;
//             return new Promise(function (n) {
//                 return __awaiter(t, void 0, void 0, function () {
//                     return __generator(this, function () {
//                         cc.tween(e)
//                             .call(function () {
//                                 n();
//                             })
//                             .to(0.2, {
//                                 scale: 1
//                             })
//                             .start();
//                         return [2];
//                     });
//                 });
//             });
//         };
//         __decorate([L(cc.Label)], t.prototype, "award", void 0);
//         __decorate([L(cc.Sprite)], t.prototype, "sprite_award_icon", void 0);
//         __decorate([L(cc.Node)], t.prototype, "award_node", void 0);
//         __decorate([L(cc.Node)], t.prototype, "btn_node", void 0);
//         __decorate([L(sp.Skeleton)], t.prototype, "ske", void 0);
//         __decorate([L(sp.Skeleton)], t.prototype, "ske2", void 0);
//         return __decorate([D], t);
//     })(AL_GAME_HUA_PopupSelf.default));
// exports.default = b;


// 引入必要的模块
const AL_GAME_HUA_AddPrefab = require("./AL_GAME_HUA_AddPrefab");
const AL_GAME_HUA_Instance = require("./AL_GAME_HUA_Instance");
const AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
const AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
const AL_GAME_HUA_PopupSelf = require("./AL_GAME_HUA_PopupSelf");
const AL_GAME_HUA_AwardConfig = require("./AL_GAME_HUA_AwardConfig");
const AL_GAME_HUA_EventConfig = require("./AL_GAME_HUA_EventConfig");
const AL_GAME_HUA_PropConfig = require("./AL_GAME_HUA_PropConfig");
const AL_GAME_HUA_CurrencyData = require("./AL_GAME_HUA_CurrencyData");
const AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
const AL_GAME_HUA_GoodsConfig = require("./AL_GAME_HUA_GoodsConfig");

// 短名字变量
const ccDecorator = cc._decorator;
const ccclass = ccDecorator.ccclass;
const property = ccDecorator.property;

/**
 * Class PopupHandler 处理与奖励相关的弹窗逻辑。
 */
@ccclass
class Award extends AL_GAME_HUA_PopupSelf.default {
    // 奖励信息
    @property(cc.Label)
    award = [];

    // 奖励图标
    @property(cc.Sprite)
    sprite_award_icon = null;

    // 奖励节点
    @property(cc.Node)
    award_node = [];

    // 按钮节点
    @property(cc.Node)
    btn_node = null;

    // 骨骼动画
    @property(sp.Skeleton)
    ske = null;

    // 额外骨骼动画
    @property(sp.Skeleton)
    ske2 = null;

    // 其他属性
    types = true;
    speed = 1500;
    gold_array_list = [];
    position_array_list = [cc.v3(-200, 95), cc.v3(0, 160), cc.v3(200, 95)];
    click_type = false;

    /**
     * 显示动作
     * @returns {Promise} 返回Promise对象
     */
    xianshi_dongzuo() {
        return Promise.resolve();
    }

    /**
     * 隐藏操作
     * @returns {Promise} 返回Promise对象
     */
    yincang_dongzuo() {
        return Promise.resolve();
    }

    /**
     * 加载开始时调用
     */
    start() {
        // 显示顶部栏
        AL_GAME_HUA_Instance.Instance.xianshi_dingbu_tiao({
            target: this.node,
            showGold: true
        });
    }

    /**
     * 组件启用时调用
     */
    onEnable() {
        // 发送隐藏游戏俱乐部事件
        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
            node: this.node,
            hide: true
        });
    }

    /**
     * 组件禁用时调用
     */
    onDisable() {
        // 发送显示游戏俱乐部事件
        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
            node: this.node,
            hide: false
        });
    }

    /**
     * 初始化奖励信息
     * @param {object} options - 奖励选项
     */
    zhixing_chushihua_chuli(options) {
        this.options = options;
        const gameDataInstance = AL_GAME_HUA_GameData.GameData.getInstance();
        const awardConfigInstance = AL_GAME_HUA_AwardConfig.AwardConfig.getInstance();

        this.ske.node.active = false;
        this.ske2.node.active = false;

        let currentAward;

        switch (options.type) {
            case AL_GAME_HUA_Define.Define.AwardType.LEVEL:
                currentAward = awardConfigInstance.genju_jishu_huoqu_jianli_xinxi(gameDataInstance.huoqu_jiangli_shuliang());
                break;
            case AL_GAME_HUA_Define.Define.AwardType.CHALLENGE:
                currentAward = awardConfigInstance.huoqu_tiaozhan_jianli_xinxi();
                break;
            case AL_GAME_HUA_Define.Define.AwardType.SPECIAL:
                currentAward = awardConfigInstance.genju_jishu_huoqu_jianli_xinxi(gameDataInstance.huoqu_teshu_jiangli_shuliang());
                break;
            case AL_GAME_HUA_Define.Define.AwardType.STAR:
                currentAward = awardConfigInstance.genju_jishu_huoqu_jianli_xinxi(gameDataInstance.huoqu_xingxing_hezi_shuliang());
                break;
        }

        // 更新奖励说明
        this.award[0].string = `x${currentAward.Award1}`;
        this.award[1].string = `x${currentAward.Number}`;

        const awardIconId = {
            0: 2,
            1: 3,
            2: 4,
            3: 5,
            4: 6,
            5: 7,
            6: 8
        }[currentAward.Award2];

        AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance()
            .jiazai_tubiao_icon(awardIconId)
            .then((spriteFrame) => {
                if (cc.isValid(this)) {
                    this.sprite_award_icon.spriteFrame = spriteFrame;
                    this.sprite_award_icon.addAutoReleaseAssets(spriteFrame);
                    AL_GAME_HUA_Common.Common.uniform_scale(this.sprite_award_icon, 80, 80);
                }
            });

        AL_GAME_HUA_Common.Common.bofang_yinxiao("box_fall", this);

        this.ske.node.active = true;
        this.ske.setAnimation(0, "show1", false);
        const animationTrack = this.ske.addAnimation(0, "show2", false);
        this.ske.addAnimation(0, "idle", true);

        this.scheduleOnce(() => {
            AL_GAME_HUA_Common.Common.bofang_yinxiao("game_openbox", this);
            this.award.forEach(award => {
                this.chuli_jiedian_yidong_donghua(award.node.parent);
            });
        }, 1);

        this.ske.setTrackCompleteListener(animationTrack, () => {
            cc.tween(this.btn_node)
                .delay(0.5)
                .by(0.15, { y: 465 }, { easing: "backOut" })
                .start();

            cc.tween(this.ske.node)
                .delay(0.5)
                .by(0.4, { y: -cc.winSize.height / 2 - 150 }, { easing: "backIn" })
                .start();
        });
    }

    /**
     * 接收奖励
     * @param {number} multiplier - 奖励倍数
     * @param {number} rewardType - 奖励类型
     */
    recieve(multiplier, rewardType) {
        const currencyDataInstance = AL_GAME_HUA_CurrencyData.CurrencyData.getInstance();
        const gameDataInstance = AL_GAME_HUA_GameData.GameData.getInstance();
        const awardConfigInstance = AL_GAME_HUA_AwardConfig.AwardConfig.getInstance();

        const count = this.options.type === AL_GAME_HUA_Define.Define.AwardType.LEVEL 
            ? gameDataInstance.huoqu_jiangli_shuliang() 
            : gameDataInstance.huoqu_xingxing_hezi_shuliang();

        const award = awardConfigInstance.genju_jishu_huoqu_jianli_xinxi(count);

        const awardGoodsMap = {
            [AL_GAME_HUA_PropConfig.PropId.TIPS_ENTER]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_magicStick,
            [AL_GAME_HUA_PropConfig.PropId.TIME_ENTER]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_overtime,
            [AL_GAME_HUA_PropConfig.PropId.STAR_ENTER]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_doubleStar,
            [AL_GAME_HUA_PropConfig.PropId.TIPS_GAME]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
            [AL_GAME_HUA_PropConfig.PropId.REFRESH_GAME]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
            [AL_GAME_HUA_PropConfig.PropId.TIME_GAME]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
            [AL_GAME_HUA_PropConfig.PropId.CHANGE_GAME]: AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush
        };

        const goodsList = [
            {
                goods: AL_GAME_HUA_GoodsConfig.GoodsId.gold,
                num: award.Award1 * multiplier
            },
            {
                goods: awardGoodsMap[award.Award2],
                num: award.Number * multiplier
            }
        ];

        AL_GAME_HUA_AddPrefab.AddPrefab.zhixing_feixing_jinbi({
            fromNode: this.award_node[0],
            count: 5,
            onEnd: () => {
                currencyDataInstance.tianjia_huobi({ type: 0, num: award.Award1 * multiplier });
                AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
                    source: this.options.type === AL_GAME_HUA_Define.Define.AwardType.STAR
                        ? AL_GAME_HUA_EventConfig.AssetsSource.starbox
                        : AL_GAME_HUA_EventConfig.AssetsSource.giftbox,
                    action: multiplier > 1
                        ? (rewardType === 2 
                            ? AL_GAME_HUA_EventConfig.AssetsAction.video_get 
                            : AL_GAME_HUA_EventConfig.AssetsAction.share_get)
                        : AL_GAME_HUA_EventConfig.AssetsAction.get,
                    goodsList
                });
                if (this.options.chuli_lingqu) {
                    this.options.chuli_lingqu();
                }
                if (cc.isValid(this)) {
                    this.close();
                }
            }
        });

        gameDataInstance.genggai_prop_shuliang(award.Award2, award.Number * multiplier);

        switch (this.options.type) {
            case AL_GAME_HUA_Define.Define.AwardType.LEVEL:
                gameDataInstance.shezhi_jiangli_shuliang();
                break;
            case AL_GAME_HUA_Define.Define.AwardType.SPECIAL:
                gameDataInstance.shezhi_teshu_jiangli_shuliang();
                break;
            case AL_GAME_HUA_Define.Define.AwardType.STAR:
                gameDataInstance.tianjia_xingxing_hezi_shuliang();
                break;
        }
    }

    /**
     * 处理按钮回调
     * @param {object} event - 事件对象
     * @param {string} buttonType - 按钮类型
     */
    handle_button_callback(event, buttonType) {
        switch (buttonType) {
            case "recieve":
                this.recieve(1, 0);
                this.xianshi_block_top();
                break;
            case "video":
                this.xianshi_block_top();
                AL_GAME_HUA_Common.Common.showVideo({
                    success: (res) => {
                        this.recieve(2, res.type);
                    },
                    fail: () => {
                        this.yincang_block_top();
                        AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_tishi_jiedian_mianban({ str: "暂无视频" });
                    }
                });
                break;
        }
    }

    /**
     * 节点移动动画
     * @param {cc.Node} node - 节点对象
     * @returns {Promise} 返回Promise对象
     */
    chuli_jiedian_yidong_donghua(node) {
        return new Promise((resolve) => {
            cc.tween(node)
                .call(() => resolve())
                .to(0.2, { scale: 1 })
                .start();
        });
    }
}

exports.default = Award;